package cn.rubintry.bug.lib.network.http.viewmodel

import androidx.lifecycle.ViewModelProvider
import com.orhanobut.hawk.Hawk

class ViewModelKey {

    companion object{
        private var key = ""

        @JvmStatic
        fun get(): String {
            val key = Hawk.get<String?>("viewModelKey") ?: ""
            return key.ifEmpty {
                try {
                    val providerClazz = ViewModelProvider::class.java
                    val keyField = providerClazz.getDeclaredField("DEFAULT_KEY")
                    keyField.isAccessible = true
                    val key = keyField.get(null) as? String
                    key?.let {
                        Hawk.put("viewModelKey", key)
                    }
                    key ?: ""
                } catch (e: Exception) {
                    ""
                }
            }
        }
    }

}